/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jm.jmsql.cloud.validation;

import com.jm.jmsql.cloud.entities.Users;
import com.jm.jmsql.cloud.rs.Message;
import com.jm.jmsql.cloud.rs.Message.Type;
import com.jm.jmsql.cloud.rs.Result;
import static com.jm.jmsql.cloud.validation.RegularExpressions.EMAIL_ADDRESS;
import static com.jm.jmsql.cloud.validation.RegularExpressions.PASSWORD;

/**
 *
 * @author Marques
 */
public class AuthenticationValidator {
    
    /**
     * Private Constants
     */
    private static final String INCORRECT_PASSWORD = "Incorrect password!";
    private static final String USERNAME_DOES_NOT_EXIST = "Username provided does not exist!";
    private static final String EMAIL_DOES_NOT_EXIST = "Email address provided does not exist!";
    private static final String USERNAME_EMPTY = "Username cannot be empty!";
    private static final String INVALID_EMAIL_ADDRESS = "Not a valid email address!";
    private static final String INVALID_PASSWORD = "Not a valid password!";
    
    /**
     * 
     * @param email
     * @param user
     * @param password
     * @return Result
     */
    public Result login(boolean email, String password, Users user) {
        if (user == null) {
            return new Result(false, new Message(Type.ERROR, email ? EMAIL_DOES_NOT_EXIST : USERNAME_DOES_NOT_EXIST));
        } else if (!user.getPassword().equals(password)) {
            return new Result(false, new Message(Type.ERROR, INCORRECT_PASSWORD));
        }
        return new Result("Login Successful!");
    }
    
    /**
     * 
     * @param username
     * @param password
     * @param email
     * @return 
     */
    public Result register(String username, String password, String email) {
        Result result = new Result();
        if (username == null ||
                username.isEmpty()) {
            result.setSuccessful(false);
            result.add(new Message(Type.ERROR, USERNAME_EMPTY));
        }
        if (!password.matches(PASSWORD)) {
            result.setSuccessful(false);
            result.add(new Message(Type.ERROR, INVALID_PASSWORD));
            result.add(new Message(Type.WARNING, "Password length of 8 to 32 characters."));
            result.add(new Message(Type.WARNING, "A digit must occur at least once."));
            result.add(new Message(Type.WARNING, "A lower case letter must occur at least once."));
            result.add(new Message(Type.WARNING, "An upper case letter must occur at least once."));
            result.add(new Message(Type.WARNING, "No whitespaces allowed."));
        }
        if (!email.matches(EMAIL_ADDRESS)) {
            result.setSuccessful(false);
            result.add(new Message(Type.ERROR, INVALID_EMAIL_ADDRESS));
            result.add(new Message(Type.WARNING, "E.G: smiths.johnson@domainian.com"));
        }
        return result;
    }
    
}
